<template>
	<view>
		<view class="all">
			<view class="body">
				<view class="box">
					<view class="left">
						姓名
					</view>
					<view class="right">
						<input type="text" placeholder="请输入你的姓名" v-model="name" />
					</view>
				</view>
				<view class="box">
					<view class="left">
						手机号
					</view>
					<view class="right">
						<input type="number" placeholder="请输入手机号" v-model="phone" maxlength="11" />
					</view>
				</view>
			</view>
			<view class="body2">
				<view class="title">
					头像
				</view>
				<view class="bottom">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="1"></u-upload>
				</view>
			</view>
		</view>
		<view class="last">
			<view v-show="!can" class="box" @click="addaides()">
				添加
			</view>
			<view v-show="can" class="box" @click="editaides()">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: "",
				phone: "",
				fileList1: [],
				work_photos: "",
				can: false,
				id: ""
			};
		},
		onLoad(option) {
			console.log(option.id);
			if (option.id) {
				this.can = true
				this.id = option.id
			}
		},
		methods: {
			//添加护工
			addaides() {
				this.getaides()
			},
			//编辑护工
			editaides() {
				this.editaides()
			},
			async editaides() {
				const res = await this.$api('/business/user/edit_support_worker', 'post', {
					name: this.name,
					phone: this.phone,
					work_photos: this.work_photos,
					id: this.id
				}).then((res) => {
					console.log(res);
					uni.$u.toast(res.data.msg)
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						}, )
					}, 1000)
				}).catch((err) => {
					console.log(err);
				})
			},
			//添加护工
			async getaides() {
				const res = await this.$api("/business/user/add_support_worker", 'post', {
					name: this.name,
					phone: this.phone,
					work_photos: this.work_photos
				}).then((res) => {
					console.log(res);
					if (res.data.code == 1) {
						uni.$u.toast(res.data.msg)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							}, )
						}, 1000)
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						// status: 'uploading',
						// message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.lykangshu.top/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							const ret = JSON.parse(res.data)
							console.log(ret.data);
							this.work_photos = ret.data.url
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.all {
		padding: 15px;
		min-height: 100vh;
		background-color: #f5f5f5;

		.body {
			// padding: 5px;
			background: #FFFFFF;
			border-radius: 10px;

			.box {
				display: flex;
				align-items: center;
				padding: 10px;
				border-bottom: 1px solid #eeeeee;

				.left {
					font-size: 14px;
					font-weight: bold;
					color: #333333;
					width: 20%;
				}

				.right {
					flex: 1;

					input {
						font-size: 14px;
						font-weight: 400;
						color: #999999;
					}
				}
			}
		}

		.body2 {
			padding: 10px;
			background: #FFFFFF;
			border-radius: 10px;
			margin: 20px 0;

			.title {
				font-size: 14px;
				font-weight: bold;
				color: #333333;
				margin: 10px 0;
			}
		}
	}

	.last {
		position: fixed;
		bottom: 0;
		width: 100%;

		background: #FFFFFF;
		box-shadow: 0px -2px 4px 1px rgba(0, 0, 0, 0.05);
		padding: 10px 10px 20px 10px;
		box-sizing: border-box;

		.box {
			background: #FB774B;
			border-radius: 40px;
			font-size: 16px;
			font-weight: bold;
			color: #FFFFFF;
			text-align: center;
			padding: 10px 0;
		}
	}
</style>